package isisv1

import (
	"fmt"
	"topobuild/utils"

	"github.com/xuri/excelize/v2"
)

/*
设备名称	设备IP地址	端口IP地址	端口名称	电路级别	Metric
B169A2	100.10.0.5	100.10.0.5/32	loopback0	level2	10
A169A2	100.10.0.3	100.10.0.3/32	loopback0	level2	10
A169A1	100.10.0.2	100.10.0.2/32	loopback0	level2	10
*/

type TableIsisv1Intf struct {
	DevName      string `json:"dev_name"`
	DevIp        string `json:"dev_ip"`
	PortIp       string `json:"port_ip"`
	PortName     string `json:"port_name"`
	CircuitLevel string `json:"circuit_level"`
	Metric       int    `json:"metric"`
}

// 将TableIsisv1Intf中的数据,写入到指定xlsx文件的interface表中
func WriteIntfXlsx(filename string, intfs []TableIsisv1Intf) {
	// 如果filename已经存在,则直接打开,否则创建一个新文件
	utils.CheckFile(filename)
	xlsx, err := excelize.OpenFile(filename)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer xlsx.Close()
	// Create a new sheet.
	index, err := xlsx.NewSheet("interface")
	if err != nil {
		fmt.Println(err)
		return
	}
	// Set value of a cell.
	xlsx.SetCellValue("interface", "A1", "设备名称")
	xlsx.SetCellValue("interface", "B1", "设备IP地址")
	xlsx.SetCellValue("interface", "C1", "端口IP地址")
	xlsx.SetCellValue("interface", "D1", "端口名称")
	xlsx.SetCellValue("interface", "E1", "电路级别")
	xlsx.SetCellValue("interface", "F1", "Metric")
	// 设置单元格样式
	// style, _ := xlsx.NewStyle(`{"alignment":{"horizontal":"center"}}`)
	// xlsx.SetCellStyle("dev", "A1", "G1", style)
	// 设置列宽
	// xlsx.SetColWidth("dev", "A", "G", 20)
	// 设置行高
	// Set active sheet of the workbook.
	xlsx.SetActiveSheet(index)
	// Save xlsx file by the given path.
	fmt.Printf("len(intfs): %v\n", len(intfs))
	for i, intf := range intfs {
		xlsx.SetCellValue("interface", fmt.Sprintf("A%d", i+2), intf.DevName)
		xlsx.SetCellValue("interface", fmt.Sprintf("B%d", i+2), intf.DevIp)
		xlsx.SetCellValue("interface", fmt.Sprintf("C%d", i+2), intf.PortIp)
		xlsx.SetCellValue("interface", fmt.Sprintf("D%d", i+2), intf.PortName)
		xlsx.SetCellValue("interface", fmt.Sprintf("E%d", i+2), intf.CircuitLevel)
		xlsx.SetCellValue("interface", fmt.Sprintf("F%d", i+2), intf.Metric)
	}
	xlsx.Save()
}
